package com.example.pinkcat.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

public class AboutPinkCat extends AppCompatActivity {

    Context context =this;
    String url="http://yx.down.fang5688.wang:8004/game/gaozhisdaosyx.apk";//下载路径 //需要
    String mVersion_name;//新版本  //需要
    String installedVersion_name;//已安装版本

    TextView textView_version;
    TextView textView_version_installed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).hide();//去掉标题栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_about_pink_cat);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }//深色信息栏
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);//允许此线程内发起网络请求


        textView_version=findViewById(R.id.pinkcatversion);
        textView_version_installed=findViewById(R.id.pinkcatversionInstalled);
        installedVersion_name=textView_version_installed.getText().toString();
        mVersion_name=textView_version.getText().toString();


        listen();
        //获取app信息
        if(GetData.getPinkCat(context)){
            url= userStaticMsg.appUrl;//获取下载连接
            textView_version_installed.setText(userStaticMsg.appVersion);//设置更新版本
        }

    }

    //按键监听
    void listen(){
        //下载监听
        findViewById(R.id.name_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (textView_version.getText().toString().equals(textView_version_installed.getText().toString())){
                    Toast.makeText(context,"已经是最新版本",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(context,"请完成下载后安装",Toast.LENGTH_SHORT).show();
                    downloadAPK();
                }
            }
        });

        //进入官网
        findViewById(R.id.goto_networke_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OpenWeb();
            }
        });

        //返回键监听
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
    //打开浏览器
    void OpenWeb(){
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse("http://pinkcat.top:8888/down.html");
        intent.setData(content_url);
        startActivity(intent);
    }

    /*
     * 下载app
     */
    protected void downloadAPK() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //Uri apksss=Uri.parse("http://yx.down.fang5688.wang:8004/game/gaozhisdaosyx.apk");
        Uri apksss=Uri.parse(url);
        intent.setDataAndType(apksss, "application/vnd.android.package-archive");
        try {
            startActivity(intent);
        }catch (ActivityNotFoundException e){
            System.out.println(e);
            Toast.makeText(context,"更新异常，请前往官网下载最新版本",Toast.LENGTH_LONG).show();
        }

    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

}